package gui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * 
 * @author Csaba Tarkanyi
 * 
 */
public class SetAmountDialog extends JDialog {

	private JLabel todo;
	private JTextField txtF;
	private JButton ok, cancel;
	private JPanel pCenter, pSouth;

	private int amount;

	public SetAmountDialog(JDialog owner, String title) {
		super(owner, title, true);
		setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
		setLocationRelativeTo(owner);
		setSize(200, 120);
		// Sets the new size by using parent's center point and the size of this
		// JDialog -> this will be always in the center of the parent.
		setLocation(getX() - (getWidth() / 2), getY() - (getHeight() / 2));
		setResizable(false);
		getContentPane().setLayout(new BorderLayout());

		pCenter = new JPanel(null);
		getContentPane().add(pCenter, BorderLayout.CENTER);

		pSouth = new JPanel(new FlowLayout(FlowLayout.CENTER));
		getContentPane().add(pSouth, BorderLayout.SOUTH);

		todo = new JLabel("Type the Amount:");
		todo.setBounds(10, 5, 120, 24);
		pCenter.add(todo);

		txtF = new JTextField();
		txtF.setBounds(10, 30, 170, 24);
		pCenter.add(txtF);

		ok = new JButton("Ok");
		ok.setPreferredSize(new Dimension(70, 22));
		// Anonymous class.
		ok.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == ok) {
					// Gets the typed amount and save it.
					setAmount(Integer.parseInt(txtF.getText()));
					SetAmountDialog.this.setVisible(false);
				}
			}
		});
		pSouth.add(ok);

		cancel = new JButton("Cancel");
		cancel.setPreferredSize(new Dimension(70, 22));
		cancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == cancel) {
					// Do nothing, just exit.
					SetAmountDialog.this.setVisible(false);
				}
			}
		});
		pSouth.add(cancel);
	}

	/**
	 * @return the amount
	 * 
	 * @author Csaba Tarkanyi
	 */
	public int getAmount() {
		return amount;
	}

	/**
	 * @param amount
	 *            the amount to set
	 * 
	 * @author Csaba Tarkanyi
	 */
	public void setAmount(int amount) {
		this.amount = amount;
	}

}